package org.example.system.service;

import org.example.system.entity.ScenicSpot;
import org.example.system.mapper.ScenicSpotMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class ScenicSpotService {
    @Autowired
    private ScenicSpotMapper scenicSpotMapper;

    public int add(ScenicSpot spot) { return scenicSpotMapper.insert(spot); }
    public int delete(Long id) { return scenicSpotMapper.deleteById(id); }
    public int update(ScenicSpot spot) { return scenicSpotMapper.update(spot); }
    public ScenicSpot findById(Long id) { return scenicSpotMapper.selectById(id); }
    public List<ScenicSpot> findAll() { return scenicSpotMapper.selectAll(); }
    public List<ScenicSpot> findByName(String name) { return scenicSpotMapper.selectByName(name); }
    public void decreaseStockByName(String name, int quantity) {
        List<ScenicSpot> spots = findByName(name);
        if (spots != null && !spots.isEmpty()) {
            ScenicSpot spot = spots.get(0);
            spot.setStock(spot.getStock() - quantity);
            scenicSpotMapper.update(spot);
        }
    }
    public void increaseStockByName(String name, int quantity) {
        List<ScenicSpot> spots = findByName(name);
        if (spots != null && !spots.isEmpty()) {
            ScenicSpot spot = spots.get(0);
            spot.setStock(spot.getStock() + quantity);
            scenicSpotMapper.update(spot);
        }
    }
} 